package com.starlinkdt.service.system.impl;

import com.starlinkdt.common.constant.HttpStatus;
import com.starlinkdt.common.domain.system.dto.OaiAccDto;
import com.starlinkdt.common.domain.system.entity.OaiAcc;
import com.starlinkdt.common.enums.DisableStatus;
import com.starlinkdt.common.exception.ServiceException;
import com.starlinkdt.common.mapper.system.OaiAccMapper;
import com.starlinkdt.common.utils.MyStringUtils;
import com.starlinkdt.common.utils.SecurityUtils;
import com.starlinkdt.service.system.IOaiAccService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * OAI账户Service业务层处理
 * 
 * @author system
 */
@Service
public class OaiAccServiceImpl implements IOaiAccService
{
    @Autowired
    private OaiAccMapper oaiAccMapper;

    private static final String accBizNoPre  = "UOA";

    private static final int accBizNoLength = 20;

    /**
     * 查询OAI账户列表
     *
     * @param oaiAccSearch OAI账户
     * @return OAI账户集合
     */
    @Override
    public List<OaiAcc> selectOaiAccList(OaiAcc oaiAccSearch) {
        return oaiAccMapper.selectOaiAccList(oaiAccSearch);
    }

    /**
     * 查询OAI账户
     *
     * @param accBizNo OAI账户唯一编码
     * @return OAI账户
     */
    @Override
    public OaiAcc selectOaiAccByAccBizNo(String accBizNo) {
        OaiAcc oaiAcc = oaiAccMapper.selectOaiAccByAccBizNo(accBizNo);
        if (oaiAcc == null) {
            throw new ServiceException("OAI账户不存在", HttpStatus.CONFLICT);
        }
        return oaiAcc;
    }

    /**
     * 新增OAI账户
     *
     * @param oaiAccDto OAI账户
     * @return 结果
     */
    @Override
    public int insertOaiAcc(OaiAccDto oaiAccDto) {
        if (oaiAccMapper.selectOaiAccByAccAppId(oaiAccDto.getAccAppId()) != null) {
            throw new ServiceException("OAI账户已存在", HttpStatus.CONFLICT);
        }

        OaiAcc oaiAcc = new OaiAcc();
        oaiAcc.setAccName(oaiAccDto.getAccName());
        oaiAcc.setAccBizNo(MyStringUtils.genBizNo(accBizNoLength, accBizNoPre));
        oaiAcc.setAccAppId(oaiAccDto.getAccAppId());
        oaiAcc.setAccAppKey(MyStringUtils.genNonceStr());
        oaiAcc.setCreateBy(SecurityUtils.getUsername());
        return oaiAccMapper.insertOaiAcc(oaiAcc);
    }

    /**
     * 修改OAI账户
     *
     * @param oaiAccDto OAI账户
     * @return 结果
     */
    @Override
    public int updateOaiAccByAccBizNo(OaiAccDto oaiAccDto) {
        if (oaiAccMapper.selectOaiAccByAccBizNo(oaiAccDto.getAccBizNo()) == null) {
            throw new ServiceException("OAI账户不存在", HttpStatus.CONFLICT);
        }

        OaiAcc oaiAcc = new OaiAcc();
        oaiAcc.setAccBizNo(oaiAccDto.getAccBizNo());
        oaiAcc.setAccName(oaiAccDto.getAccName());
        oaiAcc.setAccAppId(oaiAccDto.getAccAppId());
        oaiAcc.setUpdateBy(SecurityUtils.getUsername());
        return oaiAccMapper.updateOaiAccByAccBizNo(oaiAcc);
    }

    /**
     * 启用OAI账户
     *
     * @param accBizNos OAI账户唯一编码
     * @return 结果
     */
    @Override
    public int enableOaiAccByAccBizNos(String[] accBizNos) {
        return oaiAccMapper.updateOaiAccStatusByAccBizNos(accBizNos, DisableStatus.ENABLE.getType(), SecurityUtils.getUsername());
    }

    /**
     * 禁用OAI账户
     *
     * @param accBizNos OAI账户唯一编码
     * @return 结果
     */
    @Override
    public int disableOaiAccByAccBizNos(String[] accBizNos) {
        return oaiAccMapper.updateOaiAccStatusByAccBizNos(accBizNos, DisableStatus.DISABLE.getType(), SecurityUtils.getUsername());
    }

    /**
     * 删除OAI账户
     *
     * @param accBizNo OAI账户唯一编码
     * @return 结果
     */
    @Override
    public int deleteOaiAccByAccBizNo(String accBizNo) {
        if (oaiAccMapper.selectOaiAccByAccBizNo(accBizNo) == null) {
            throw new ServiceException("OAI账户不存在", HttpStatus.CONFLICT);
        }

        return oaiAccMapper.deleteOaiAccByAccBizNo(accBizNo, SecurityUtils.getUsername());
    }

    /**
     * 批量删除OAI账户
     *
     * @param accBizNos OAI账户唯一编码
     * @return 结果
     */
    @Override
    public int deleteOaiAccByAccBizNos(String[] accBizNos) {
        return oaiAccMapper.deleteOaiAccByAccBizNos(accBizNos, SecurityUtils.getUsername());
    }

}
